﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using Domain.Interface;
using Domain.Model;
using Ds.DataBase;
using Ds.DataBase.Enum;
using Ds.DataBase.Model;
using Ds.DataBase.Model.PersonalAssess;
using Newtonsoft.Json;

namespace Domain.RealiZation
{
    public class PingShenCaoZuotmpl : IPingShenCaoZuo
    {
        public string GongZuoJiHuaYuanGong(string datetimes, int mbid, string username)
        {

            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var zw = db.ZhiWeiContext.ToList();
                var riqi = DateTime.Parse(datetimes);
                List<int> nrids = db.KaoHeFanWeisContext.Where(s => s.username == username).Select(s => s.NeiRong.nrid).ToList();
                var tmp = from aa in db.KaoHeFanWeisContext where aa.username == username select new { aa.ZhiWeis };
                List<int> zwids = new List<int>();
                foreach (var item in tmp)
                {
                    zwids.AddRange(item.ZhiWeis.Select(s => s.zwid));
                }

                var users = db.JiaoZhiGongInfoContext.Where(s => zwids.Any(p => p == s.kszw.zhiwu.zwid));
                var tmp2 = db.MyKaoHeBiaoContext.Where(s => (s.khbzt == 考核表状态.提交审核 || s.khbzt == 考核表状态.通过审核) && s.mbid == mbid && s.sssj.Year == riqi.Year && s.sssj.Month == riqi.Month && s.MyKaoHeBiaoTongGuoJiLus.All(p => p.usernanme != username));
                List<MyKaoHeBiao> kaohebiao = new List<MyKaoHeBiao>();
                foreach (var item in tmp2)
                {
                    if (item.MyKaoHeBiaoOnes.Any(s => nrids.Exists(t => t == s.mbnrid)))
                    {
                        kaohebiao.Add(item);
                    }
                }
                List<string> usernames = new List<string>();
                foreach (var item in users)
                {
                    usernames.Add(item.Users.username);
                }
                var reluse = kaohebiao.Where(s => usernames.Exists(p => p == s.username));
                List<Domain.Model.Domain_MyKaoHeBiao> myKaoHeBiaos = new List<Domain_MyKaoHeBiao>();
                foreach (var item in reluse)
                {
                    Domain_MyKaoHeBiao model = new Domain_MyKaoHeBiao();
                    model.MyKaoHeBiaoOnes = new List<Domain_MyKaoHeBiaoOne>();
                    model.cjsj = item.cjsj;
                    model.khbzt = (int)item.khbzt;
                    model.mbid = item.mbid;
                    model.mkhbid = item.mkhbid;
                    model.sssj = item.sssj;
                    model.username = item.username;
                    model.xingming = db.JiaoZhiGongInfoContext.SingleOrDefault(s => s.Users.username == item.username)
                        .XingMing;
                    //foreach (var item2 in item.MyKaoHeBiaoOnes.Where(s => nrids.Exists(t => t == s.mbnrid)))
                    //{
                    //    Domain_MyKaoHeBiaoOne s=new Domain_MyKaoHeBiaoOne();
                    //    s.fz = item2.fz;
                    //    s.htmlstr = item2.htmlstr;
                    //    s.mbid = item2.mbnrid;
                    //    s.mbnr = item2.mbnr;
                    //    s.mbtype = item2.mbtype;
                    //    s.oneid = item2.oneid;
                    //    s.sx = item2.sx;
                    //    s.MyKaoHeBiaoTwos=new List<Domain_MyKaoHeBiaotwo>();
                    //    foreach (var item3 in item2.MyKaoHeBiaoTwos)
                    //    {
                    //        Domain_MyKaoHeBiaotwo ss=new Domain_MyKaoHeBiaotwo();
                    //        ss.mbid = item3.mbid;
                    //        ss.bz = item3.bz;
                    //        ss.fz = item3.fz;
                    //        ss.gzjhkssj = item3.gzjhkssj;
                    //        ss.gzjhwcsj = item3.gzjhwcsj;
                    //        ss.htmlstr = item3.htmlstr;
                    //        ss.mbnr = item3.mbnr;
                    //        ss.sx = item3.sx;
                    //        ss.twoid = item3.twoid;
                    //        ss.zf = item3.zf;
                    //        s.MyKaoHeBiaoTwos.Add(ss);
                    //    }
                    //    model.MyKaoHeBiaoOnes.Add(s);
                    //}

                    myKaoHeBiaos.Add(model);
                }

                return JsonConvert.SerializeObject(myKaoHeBiaos);
            }
        }
        /// <summary>
        /// 获取可审核的考核表
        /// </summary>
        /// <param name="mbid"></param>
        /// <param name="username"></param>
        /// <returns></returns>
        public string GetKaoHeBiaosByKeShenHe(string datetimes, int mbid, string username)
        {

            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var zw = db.ZhiWeiContext.ToList();
                var riqi = DateTime.Parse(datetimes);
                List<int> nrids = db.KaoHeFanWeisContext.Where(s => s.username == username).Select(s => s.NeiRong.nrid).ToList();
                var tmp = from aa in db.KaoHeFanWeisContext where aa.username == username select new { aa.ZhiWeis };
                List<int> zwids = new List<int>();
                foreach (var item in tmp)
                {
                    zwids.AddRange(item.ZhiWeis.Select(s => s.zwid));
                }

                var users = db.JiaoZhiGongInfoContext.Where(s => zwids.Any(p => p == s.kszw.zhiwu.zwid));

                List<MyKaoHeBiao> kaohebiao = new List<MyKaoHeBiao>();
                var data = db.MyKaoHeBiaoContext.Where(s =>
                    s.mbid == mbid && s.sssj.Year == riqi.Year && s.sssj.Month == riqi.Month && s.MyKaoHeBiaoTongGuoJiLus.All(p => p.usernanme != username));// (s.khbzt == 考核表状态.提交审核 || s.khbzt == 考核表状态.通过审核) &&
                foreach (var item in data)
                {
                    if (item.MyKaoHeBiaoOnes.Any(s => nrids.Exists(t => t == s.mbnrid)))
                    {
                        kaohebiao.Add(item);
                    }
                }
                List<string> usernames = new List<string>();
                foreach (var item in users)
                {
                    usernames.Add(item.Users.username);
                }
                var reluse = kaohebiao.Where(s => usernames.Exists(p => p == s.username));
                List<Domain.Model.Domain_MyKaoHeBiao> myKaoHeBiaos = new List<Domain_MyKaoHeBiao>();
                foreach (var item in reluse)
                {
                    Domain_MyKaoHeBiao model = new Domain_MyKaoHeBiao();
                    var tmpuser = db.JiaoZhiGongInfoContext.SingleOrDefault(s => s.Users.username == item.username);
                    model.MyKaoHeBiaoOnes = new List<Domain_MyKaoHeBiaoOne>();
                    model.cjsj = item.cjsj;
                    model.khbzt = (int)item.khbzt;
                    model.mbid = item.mbid;
                    model.mkhbid = item.mkhbid;
                    model.sssj = item.sssj;
                    model.username = item.username;
                    model.xingming = tmpuser.XingMing;
                    model.ksmc = tmpuser.keshi.ksmc;
                    model.zwmc = tmpuser.kszw.zhiwu.zwmc;
                    myKaoHeBiaos.Add(model);
                }

                return JsonConvert.SerializeObject(myKaoHeBiaos);
            }
        }
        public string GetAllIngKaoHeBiaos(int mbid, string username)
        {

            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                List<int> nrids = db.KaoHeFanWeisContext.Where(s => s.username == username).Select(s => s.NeiRong.nrid).ToList();
                var tmp = from aa in db.KaoHeFanWeisContext where aa.username == username select new { aa.ZhiWeis };
                List<int> zwids = new List<int>();
                foreach (var item in tmp)
                {
                    zwids.AddRange(item.ZhiWeis.Select(s => s.zwid));
                }

                var users = db.JiaoZhiGongInfoContext.Where(s => zwids.Any(p => p == s.kszw.zhiwu.zwid));
                var tmp2 = db.MyKaoHeBiaoContext.Where(s => (s.khbzt == 考核表状态.创建 || s.khbzt == 考核表状态.编辑中 || s.khbzt == 考核表状态.提交审核 || s.khbzt == 考核表状态.通过审核) && s.MyKaoHeBiaoOnes.Any(p => nrids.Any(w => w == p.mbnrid)));
                List<MyKaoHeBiao> kaohebiao = new List<MyKaoHeBiao>();
                foreach (var item in db.MyKaoHeBiaoContext.Where(s => (s.khbzt == 考核表状态.创建 || s.khbzt == 考核表状态.编辑中 || s.khbzt == 考核表状态.提交审核 || s.khbzt == 考核表状态.通过审核) && s.mbid == mbid))
                {
                    if (item.MyKaoHeBiaoOnes.Any(s => nrids.Exists(t => t == s.mbnrid)))
                    {
                        kaohebiao.Add(item);
                    }
                }
                List<string> usernames = new List<string>();
                foreach (var item in users)
                {
                    usernames.Add(item.Users.username);
                }
                var reluse = kaohebiao.Where(s => usernames.Exists(p => p == s.username));
                List<Domain.Model.Domain_MyKaoHeBiao> myKaoHeBiaos = new List<Domain_MyKaoHeBiao>();
                foreach (var item in reluse)
                {
                    Domain_MyKaoHeBiao model = new Domain_MyKaoHeBiao();
                    var tmpuser = db.JiaoZhiGongInfoContext.SingleOrDefault(s => s.Users.username == item.username);
                    model.MyKaoHeBiaoOnes = new List<Domain_MyKaoHeBiaoOne>();
                    model.cjsj = item.cjsj;
                    model.khbzt = (int)item.khbzt;
                    model.mbid = item.mbid;
                    model.mkhbid = item.mkhbid;
                    model.sssj = item.sssj;
                    model.username = item.username;
                    model.xingming = tmpuser.XingMing;
                    model.ksmc = tmpuser.keshi.ksmc;
                    model.zwmc = tmpuser.kszw.zhiwu.zwmc;
                    //foreach (var item2 in item.MyKaoHeBiaoOnes.Where(s => nrids.Exists(t => t == s.mbnrid)))
                    //{
                    //    Domain_MyKaoHeBiaoOne s=new Domain_MyKaoHeBiaoOne();
                    //    s.fz = item2.fz;
                    //    s.htmlstr = item2.htmlstr;
                    //    s.mbid = item2.mbnrid;
                    //    s.mbnr = item2.mbnr;
                    //    s.mbtype = item2.mbtype;
                    //    s.oneid = item2.oneid;
                    //    s.sx = item2.sx;
                    //    s.MyKaoHeBiaoTwos=new List<Domain_MyKaoHeBiaotwo>();
                    //    foreach (var item3 in item2.MyKaoHeBiaoTwos)
                    //    {
                    //        Domain_MyKaoHeBiaotwo ss=new Domain_MyKaoHeBiaotwo();
                    //        ss.mbid = item3.mbid;
                    //        ss.bz = item3.bz;
                    //        ss.fz = item3.fz;
                    //        ss.gzjhkssj = item3.gzjhkssj;
                    //        ss.gzjhwcsj = item3.gzjhwcsj;
                    //        ss.htmlstr = item3.htmlstr;
                    //        ss.mbnr = item3.mbnr;
                    //        ss.sx = item3.sx;
                    //        ss.twoid = item3.twoid;
                    //        ss.zf = item3.zf;
                    //        s.MyKaoHeBiaoTwos.Add(ss);
                    //    }
                    //    model.MyKaoHeBiaoOnes.Add(s);
                    //}

                    myKaoHeBiaos.Add(model);
                }

                return JsonConvert.SerializeObject(myKaoHeBiaos);
            }
        }
        //public string GetKaoHeBiaoInfo(int mkhbid, string username)
        //{
        //    using (UserInfoClassContext db = new UserInfoClassContext())
        //    {
        //        var zw = db.ZhiWeiContext.ToList();
        //        List<int> nrids = db.KaoHeFanWeisContext.Where(s => s.username == username).Select(s => s.NeiRong.nrid).ToList();
        //        var data = db.MyKaoHeBiaoContext.SingleOrDefault(s => s.mkhbid == mkhbid);
        //        var user = db.JiaoZhiGongInfoContext.SingleOrDefault(s => s.Users.username == data.username);
        //        Domain_MyKaoHeBiao model = new Domain_MyKaoHeBiao();
        //        model.MyKaoHeBiaoOnes = new List<Domain_MyKaoHeBiaoOne>();
        //        model.cjsj = data.cjsj;
        //        model.khbzt = (int)data.khbzt;
        //        model.mbid = data.mbid;
        //        model.mkhbid = data.mkhbid;
        //        model.sssj = data.sssj;
        //        model.username = user.Users.username;
        //        model.xingming = user.XingMing;
        //        model.ksid = user.keshi.ksid;
        //        model.ksmc = user.keshi.ksmc;
        //        model.zwid = user.kszw.zhiwu.zwid;
        //        model.zwmc = user.kszw.zhiwu.zwmc;
        //        foreach (var item2 in data.MyKaoHeBiaoOnes.Where(s => nrids.Exists(t => t == s.mbnrid)))
        //        {
        //            Domain_MyKaoHeBiaoOne s = new Domain_MyKaoHeBiaoOne();
        //            s.fz = item2.fz;
        //            s.htmlstr = item2.htmlstr;
        //            s.mbid = item2.mbnrid;
        //            s.mbnr = item2.mbnr;
        //            s.mbtype = item2.mbtype;
        //            s.oneid = item2.oneid;
        //            s.sx = item2.sx;
        //            s.MyKaoHeBiaoTwos = new List<Domain_MyKaoHeBiaotwo>();
        //            foreach (var item3 in item2.MyKaoHeBiaoTwos)
        //            {
        //                Domain_MyKaoHeBiaotwo ss = new Domain_MyKaoHeBiaotwo();
        //                ss.mbid = item3.mbid;
        //                ss.bz = item3.bz;
        //                ss.fz = item3.fz;
        //                ss.gzjhkssj = item3.gzjhkssj;
        //                ss.gzjhwcsj = item3.gzjhwcsj;
        //                ss.gzjhwcsjstr = item3.gzjhwcsjstr;
        //                ss.htmlstr = item3.htmlstr;
        //                ss.mbnr = item3.mbnr;
        //                ss.sx = item3.sx;
        //                ss.twoid = item3.twoid;
        //                ss.zf = item3.zf;
        //                s.MyKaoHeBiaoTwos.Add(ss);
        //            }
        //            model.MyKaoHeBiaoOnes.Add(s);
        //        }

        //        return JsonConvert.SerializeObject(model);
        //    }
        //}
        public string GetKaoHeBiaoInfoGongZuoJiHua(int mkhbid, string username)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var zw = db.ZhiWeiContext.ToList();
                List<int> nrids = db.KaoHeFanWeisContext.Where(s => s.username == username).Select(s => s.NeiRong.nrid).ToList();
                var data = db.MyKaoHeBiaoContext.SingleOrDefault(s => s.mkhbid == mkhbid);
                var userinfo = db.JiaoZhiGongInfoContext.SingleOrDefault(s => s.Users.username == data.username);
                Domain_MyKaoHeBiao model = new Domain_MyKaoHeBiao();
                model.MyKaoHeBiaoOnes = new List<Domain_MyKaoHeBiaoOne>();
                model.cjsj = data.cjsj;
                model.khbzt = (int)data.khbzt;
                model.mbid = data.mbid;
                model.mkhbid = data.mkhbid;
                model.sssj = data.sssj;
                model.username = data.username;
                model.xingming = userinfo.XingMing;
                model.ksmc = userinfo.keshi.ksmc;
                model.ksid = userinfo.keshi.ksid;
                model.zwmc = userinfo.kszw.zhiwu.zwmc;
                model.zwid = userinfo.kszw.zhiwu.zwid;
                foreach (var item2 in data.MyKaoHeBiaoOnes.Where(s => nrids.Exists(t => t == s.mbnrid) && s.mbtype == 1))
                {
                    Domain_MyKaoHeBiaoOne s = new Domain_MyKaoHeBiaoOne();
                    s.fz = item2.fz;
                    s.htmlstr = item2.htmlstr;
                    s.mbid = item2.mbnrid;
                    s.mbnr = item2.mbnr;
                    s.mbtype = item2.mbtype;
                    s.oneid = item2.oneid;
                    s.sx = item2.sx;
                    s.MyKaoHeBiaoTwos = new List<Domain_MyKaoHeBiaotwo>();
                    foreach (var item3 in item2.MyKaoHeBiaoTwos)
                    {
                        Domain_MyKaoHeBiaotwo ss = new Domain_MyKaoHeBiaotwo();
                        ss.mbid = item3.mbid;
                        ss.bz = item3.bz;
                        ss.fz = item3.fz;
                        ss.gzjhkssj = item3.gzjhkssj;
                        ss.gzjhwcsj = item3.gzjhwcsj;
                        ss.gzjhwcsjstr = item3.gzjhwcsjstr;
                        ss.htmlstr = item3.htmlstr;
                        ss.gzbz = item3.gzbz;
                        ss.mbnr = item3.mbnr;
                        ss.sx = item3.sx;
                        ss.twoid = item3.twoid;
                        //ss.zf = item3.zf;
                        ss.gzjhwcqk = item3.gzjhwcqk;
                        ss.zp = item3.zp;
                        ss.MyKaoHeBiaotwoFuJianses = new List<Domain_MyKaoHeBiaotwo_FuJians>();
                        foreach (var ssfujian in item3.MyKaoHeBiaoTwoFuJianses)
                        {
                            var fujian = new Domain_MyKaoHeBiaotwo_FuJians();
                            fujian.bz = ssfujian.bz;
                            fujian.fileHash = ssfujian.fileHash;
                            fujian.fileguid = ssfujian.fileguid;
                            fujian.fjid = ssfujian.fjid;
                            fujian.fjmc = ssfujian.fjmc;
                            fujian.path = ssfujian.path;
                            ss.MyKaoHeBiaotwoFuJianses.Add(fujian);
                        }
                        if (item3.MyKaoHeBiaoTwoPingFens != null)
                        {
                            if (item3.MyKaoHeBiaoTwoPingFens.Count > 0)
                            {
                                //ss.zf = ss.zf / item3.MyKaoHeBiaoTwoPingFens.Count;
                                decimal fz = item3.MyKaoHeBiaoTwoPingFens.Sum(sm => sm.fz);
                                int c = item3.MyKaoHeBiaoTwoPingFens.Count;
                                decimal f = 0m;
                                if (c > 0)
                                {
                                    f = fz / c;
                                }
                                decimal d = decimal.Round(f, 2);
                                ss.zf = d;
                            }
                            else
                            {
                                ss.zf = 0m;
                            }
                        }


                        s.MyKaoHeBiaoTwos.Add(ss);
                    }
                    model.MyKaoHeBiaoOnes.Add(s);
                }

                return JsonConvert.SerializeObject(model);
            }
        }
        public void UpdateKaoHeBiao_TongGuo(int mkhbid, string username)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.MyKaoHeBiaoContext.SingleOrDefault(s => s.mkhbid == mkhbid);
                data.khbzt = 考核表状态.通过审核;
                MyKaoHeBiao_TongGuoJiLu model = new MyKaoHeBiao_TongGuoJiLu();
                model.MyKaoHeBiao = data;
                model.cjsj = DateTime.Now;
                model.usernanme = username;
                model.ydsj = null;
                model.yijian = "通过";
                model.yjzt = false;
                if (data.MyKaoHeBiaoTongGuoJiLus == null)
                {
                    data.MyKaoHeBiaoTongGuoJiLus = new List<MyKaoHeBiao_TongGuoJiLu>();
                }
                data.MyKaoHeBiaoTongGuoJiLus.Add(model);
                db.SaveChanges();
            }
        }

        public void UpdateKaoHeBiao_TuiHui(int mkhbid, string username, string yijian)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.MyKaoHeBiaoContext.SingleOrDefault(s => s.mkhbid == mkhbid);
                data.khbzt = 考核表状态.编辑中;
                MyKaoHeBiao_BoHuiYiJian model = new MyKaoHeBiao_BoHuiYiJian();
                model.MyKaoHeBiao = data;
                model.cjsj = DateTime.Now;
                model.usernanme = username;
                model.ydsj = null;
                model.yijian = yijian;
                model.yjzt = MyKaoHeBiao_BoHuiYiJian.意见状态.未读;
                if (data.MyKaoHeBiaoBoHuiYiJians == null)
                {
                    data.MyKaoHeBiaoBoHuiYiJians = new List<MyKaoHeBiao_BoHuiYiJian>();
                }
                data.MyKaoHeBiaoBoHuiYiJians.Add(model);
                db.SaveChanges();
            }
        }

        public void CreateKaoHeBiaoJiXiao(int ksid, string username, string DateTimes)
        {
            //using (UserInfoClassContext db = new UserInfoClassContext())
            //{
            //    var sj = DateTime.Parse(DateTimes);

            //    var data = db.JiaoZhiGongInfoContext.Where(s => s.keshi.ksid == ksid).ToList();

            //    foreach (var item in data)
            //    {
            //        var kaohebiao = db.MyKaoHeBiaoContext.FirstOrDefault(s => s.username == item.Users.username && s.khbzt != 考核表状态.考核完成 && s.khbzt != 考核表状态.考核完成 && s.khbzt != 考核表状态.通过审核 && s.sssj.Year == sj.Year && s.sssj.Month == sj.Month);
            //        if (kaohebiao != null)
            //        {
            //            var jx = item.zw.MoBans.FirstOrDefault(s => s.mbtype == 模板类型.绩效考核);
            //            int jxmbid = 0;
            //            if (jx != null)
            //            {
            //                jxmbid = jx.mbid;
            //            }
            //            KaoHeBiaoRiZhi tmp = db.KaoHeBiaoRiZhiContext.SingleOrDefault(s => s.mhkbid == kaohebiao.mkhbid);
            //            if (tmp != null)
            //            {
            //                tmp.khbzt = kaohebiao.khbzt;

            //            }
            //            else
            //            {
            //                tmp = new KaoHeBiaoRiZhi();
            //                tmp.khbzt = kaohebiao.khbzt;
            //                tmp.mhkbid = kaohebiao.mkhbid;
            //                tmp.cjsj = DateTime.Now;
            //                db.KaoHeBiaoRiZhiContext.Add(tmp);
            //            }
            //            kaohebiao.khbzt = 考核表状态.通过审核;
            //            var jixiao = db.JiXiaoContext.SingleOrDefault(s => s.mbid == jxmbid);
            //            if (jixiao != null)
            //            {
            //                foreach (var item2 in jixiao.JiXiaoNeiRongs)
            //                {
            //                    MyKaoHeBiao_one one = new MyKaoHeBiao_one();
            //                    one.MyKaoHeBiao = kaohebiao;
            //                    one.MyKaoHeBiaoTwos = new List<MyKaoHeBiao_two>();
            //                    one.fz = item2.jxfz;
            //                    one.htmlstr = item2.jxhtml;
            //                    one.mbnr = item2.nr;
            //                    one.mbnrid = item2.nrid;
            //                    one.mbtype = 2;
            //                    one.sx = item2.jxsx + 1;//临时处理，让绩效表在工作计划下面
            //                    foreach (var item3 in item2.JiXiaoSons)
            //                    {
            //                        MyKaoHeBiao_two two = new MyKaoHeBiao_two();
            //                        two.mbid = item3.jxsid;
            //                        two.fz = item3.jxfz;
            //                        two.htmlstr = item3.jxshtml;
            //                        two.mbnr = item3.jxsnr;
            //                        two.sx = item3.jxssx;
            //                        two.gzjhkssj = DateTime.MaxValue;
            //                        two.gzjhwcsj = DateTime.MaxValue;
            //                        two.gzjhwcsjstr = "";
            //                        one.MyKaoHeBiaoTwos.Add(two);
            //                    }
            //                    kaohebiao.MyKaoHeBiaoOnes.Add(one);
            //                }
            //            }
            //        }

            //    }
            //    db.SaveChanges();
            //}
        }

        public void DelKaoHeBiaoKeShi(int ksid, string username)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.JiaoZhiGongInfoContext.Where(s => s.keshi.ksid == ksid).ToList();
                foreach (var item in data)
                {
                    var kaohebiao = db.MyKaoHeBiaoContext.FirstOrDefault(s => s.username == item.Users.username && s.khbzt == 考核表状态.通过审核);
                    var rizhi = db.KaoHeBiaoRiZhiContext.SingleOrDefault(s => s.mhkbid == kaohebiao.mkhbid);
                    kaohebiao.khbzt = rizhi.khbzt;
                    foreach (var item2 in kaohebiao.MyKaoHeBiaoOnes.Where(s => s.mbtype == 2))
                    {
                        foreach (var item3 in item2.MyKaoHeBiaoTwos.ToList())
                        {
                            item2.MyKaoHeBiaoTwos.Remove(item3);
                        }
                        kaohebiao.MyKaoHeBiaoOnes.Remove(item2);
                    }
                    db.Entry(rizhi).State = EntityState.Deleted;
                }

                db.SaveChanges();
            }
        }

        public string GetKaoHeFanWeiRenYuan(string username)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var zw = db.ZhiWeiContext.ToList();
                var tmp = from aa in db.KaoHeFanWeisContext where aa.username == username select new { aa.ZhiWeis };
                List<int> zwids = new List<int>();
                foreach (var item in tmp)
                {
                    zwids.AddRange(item.ZhiWeis.Select(s => s.zwid));
                }
                var query = db.JiaoZhiGongInfoContext.Where(s => zwids.Any(p => p.Equals(s.kszw.zhiwu.zwid)));
                List<Domain_JiaoZhiGong> lst = new List<Domain_JiaoZhiGong>();
                foreach (var item in query)
                {
                    var kaohebiao = db.MyKaoHeBiaoContext.FirstOrDefault(s => s.khbzt == 考核表状态.通过审核 && s.username == item.Users.username);
                    if (kaohebiao != null && kaohebiao.MyKaoHeBiaoOnes.Any(s =>
                            s.MyKaoHeBiaoTwos.Any(p => p.MyKaoHeBiaoTwoPingFens.Any(t => t.pfusername == username))))
                    {

                    }
                    else
                    {
                        Domain_JiaoZhiGong model = new Domain_JiaoZhiGong();
                        model.keshi = new Domain_KeShi();
                        model.keshi.ksid = item.keshi.ksid;
                        model.keshi.ksmc = item.keshi.ksmc;
                        model.ShouJiHao = item.ShouJiHao;
                        model.XingBie = item.XingBie;
                        model.XingMing = item.XingMing;
                        model.userinfoId = item.Users.userinfoId.ToString();
                        model.username = item.Users.username;
                        model.zgid = item.zgid;
                        model.zwid = item.kszw.zhiwu.zwid;
                        model.zwmc = item.kszw.zhiwu.zwmc;
                        lst.Add(model);
                    }

                }

                var tmprenyuan = db.KaoHeFanWeiRenYuansContext.Where(s => s.username == username).SelectMany(p => p.JiaoZhiGongInfos).Distinct();
                List<string> userid = new List<string>();
                foreach (var item in tmprenyuan)
                {
                    userid.Add(item.username);
                }
                query = db.JiaoZhiGongInfoContext.Where(s => userid.Contains(s.Users.username));
                foreach (var item in query)
                {
                    var kaohebiao = db.MyKaoHeBiaoContext.FirstOrDefault(s => s.khbzt == 考核表状态.通过审核 && s.username == item.Users.username);
                    if (kaohebiao != null && kaohebiao.MyKaoHeBiaoOnes.Any(s =>
                            s.MyKaoHeBiaoTwos.Any(p => p.MyKaoHeBiaoTwoPingFens.Any(t => t.pfusername == username))))
                    {

                    }
                    else
                    {
                        Domain_JiaoZhiGong model = new Domain_JiaoZhiGong();
                        model.keshi = new Domain_KeShi();
                        model.keshi.ksid = item.keshi.ksid;
                        model.keshi.ksmc = item.keshi.ksmc;
                        model.ShouJiHao = item.ShouJiHao;
                        model.XingBie = item.XingBie;
                        model.XingMing = item.XingMing;
                        model.userinfoId = item.Users.userinfoId.ToString();
                        model.username = item.Users.username;
                        model.zgid = item.zgid;
                        model.zwid = item.kszw.zhiwu.zwid;
                        model.zwmc = item.kszw.zhiwu.zwmc;
                        if (lst.All(s => s.username != item.Users.username))
                        {
                            lst.Add(model);
                        }
                    }
                }
                return JsonConvert.SerializeObject(lst);
            }
        }

        public string GetKaoHeBiaoInfoByusername(string ry, string username)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                //var zw = db.ZhiWeiContext.ToList();
                //var lst = db.KaoHeFanWeisContext.Where(s => s.username == username);
                //List<int> nrids = new List<int>();
                //var data = db.MyKaoHeBiaoContext.FirstOrDefault(s => s.username == ry && s.khbzt == 考核表状态.通过审核);
                //var user = db.JiaoZhiGongInfoContext.SingleOrDefault(s => s.Users.username == data.username);
                //foreach (var item in lst)
                //{
                //    nrids.AddRange(from item2 in item.ZhiWeis where item2.zwid == user.kszw.zhiwu.zwid select item.NeiRong.nrid);
                //}
                //Domain_MyKaoHeBiao model = new Domain_MyKaoHeBiao();
                //model.MyKaoHeBiaoOnes = new List<Domain_MyKaoHeBiaoOne>();
                //model.cjsj = data.cjsj;
                //model.khbzt = (int)data.khbzt;
                //model.mbid = data.mbid;
                //model.mkhbid = data.mkhbid;
                //model.sssj = data.sssj;
                //model.username = user.Users.username;
                //model.xingming = user.XingMing;
                //model.ksid = user.keshi.ksid;
                //model.ksmc = user.keshi.ksmc;
                //model.zwid = user.kszw.zhiwu.zwid;
                //model.zwmc = user.kszw.zhiwu.zwmc;
                //foreach (var item2 in data.MyKaoHeBiaoOnes.Where(s => nrids.Contains(s.mbnrid)))
                //{
                //    Domain_MyKaoHeBiaoOne s = new Domain_MyKaoHeBiaoOne();
                //    s.fz = item2.fz;
                //    s.htmlstr = item2.htmlstr;
                //    s.mbid = item2.mbnrid;
                //    s.mbnr = item2.mbnr;
                //    s.mbtype = item2.mbtype;
                //    s.oneid = item2.oneid;
                //    s.sx = item2.sx;
                //    s.MyKaoHeBiaoTwos = new List<Domain_MyKaoHeBiaotwo>();
                //    foreach (var item3 in item2.MyKaoHeBiaoTwos)
                //    {
                //        Domain_MyKaoHeBiaotwo ss = new Domain_MyKaoHeBiaotwo();
                //        ss.mbid = item3.mbid;
                //        ss.bz = item3.bz;
                //        ss.fz = item3.fz;
                //        ss.gzjhkssj = item3.gzjhkssj;
                //        ss.gzjhwcsj = item3.gzjhwcsj;
                //        ss.gzjhwcsjstr = item3.gzjhwcsjstr;
                //        ss.htmlstr = item3.htmlstr;
                //        ss.gzbz = item3.gzbz;
                //        ss.gzjhwcqk = item3.gzjhwcqk;
                //        ss.zp = item3.zp;
                //        ss.mbnr = item3.mbnr;
                //        ss.sx = item3.sx;
                //        ss.twoid = item3.twoid;
                //        ss.zf = item3.zf;
                //        ss.MyKaoHeBiaotwoFuJianses = new List<Domain_MyKaoHeBiaotwo_FuJians>();
                //        foreach (var ssfujian in item3.MyKaoHeBiaoTwoFuJianses)
                //        {
                //            var fujian = new Domain_MyKaoHeBiaotwo_FuJians();
                //            fujian.bz = ssfujian.bz;
                //            fujian.fileHash = ssfujian.fileHash;
                //            fujian.fileguid = ssfujian.fileguid;
                //            fujian.fjid = ssfujian.fjid;
                //            fujian.fjmc = ssfujian.fjmc;
                //            fujian.path = ssfujian.path;
                //            ss.MyKaoHeBiaotwoFuJianses.Add(fujian);
                //        }
                //        s.MyKaoHeBiaoTwos.Add(ss);
                //    }
                //    model.MyKaoHeBiaoOnes.Add(s);
                //}

                //if (model.MyKaoHeBiaoOnes == null || model.MyKaoHeBiaoOnes.Count == 0)
                //{
                //    var tmpry = db.KaoHeFanWeiRenYuansContext.Where(s => s.username == username);
                //    data = db.MyKaoHeBiaoContext.FirstOrDefault(s => s.username == ry && s.khbzt == 考核表状态.通过审核);
                //    user = db.JiaoZhiGongInfoContext.SingleOrDefault(s => s.Users.username == data.username);
                //    foreach (var item in tmpry)
                //    {
                //        nrids.Add(item.NeiRong.nrid);
                //    }
                //    model = new Domain_MyKaoHeBiao();
                //    model.MyKaoHeBiaoOnes = new List<Domain_MyKaoHeBiaoOne>();
                //    model.cjsj = data.cjsj;
                //    model.khbzt = (int)data.khbzt;
                //    model.mbid = data.mbid;
                //    model.mkhbid = data.mkhbid;
                //    model.sssj = data.sssj;
                //    model.username = user.Users.username;
                //    model.xingming = user.XingMing;
                //    model.ksid = user.keshi.ksid;
                //    model.ksmc = user.keshi.ksmc;
                //    model.zwid = user.kszw.zhiwu.zwid;
                //    model.zwmc = user.kszw.zhiwu.zwmc;
                //    foreach (var item2 in data.MyKaoHeBiaoOnes.Where(s => nrids.Contains(s.mbnrid)))
                //    {
                //        Domain_MyKaoHeBiaoOne s = new Domain_MyKaoHeBiaoOne();
                //        s.fz = item2.fz;
                //        s.htmlstr = item2.htmlstr;
                //        s.mbid = item2.mbnrid;
                //        s.mbnr = item2.mbnr;
                //        s.mbtype = item2.mbtype;
                //        s.oneid = item2.oneid;
                //        s.sx = item2.sx;
                //        s.MyKaoHeBiaoTwos = new List<Domain_MyKaoHeBiaotwo>();
                //        foreach (var item3 in item2.MyKaoHeBiaoTwos)
                //        {
                //            Domain_MyKaoHeBiaotwo ss = new Domain_MyKaoHeBiaotwo();
                //            ss.mbid = item3.mbid;
                //            ss.bz = item3.bz;
                //            ss.fz = item3.fz;
                //            ss.gzjhkssj = item3.gzjhkssj;
                //            ss.gzjhwcsj = item3.gzjhwcsj;
                //            ss.gzjhwcsjstr = item3.gzjhwcsjstr;
                //            ss.htmlstr = item3.htmlstr;
                //            ss.gzbz = item3.gzbz;
                //            ss.gzjhwcqk = item3.gzjhwcqk;
                //            ss.zp = item3.zp;
                //            ss.mbnr = item3.mbnr;
                //            ss.sx = item3.sx;
                //            ss.twoid = item3.twoid;
                //            ss.zf = item3.zf;
                //            ss.MyKaoHeBiaotwoFuJianses = new List<Domain_MyKaoHeBiaotwo_FuJians>();
                //            foreach (var ssfujian in item3.MyKaoHeBiaoTwoFuJianses)
                //            {
                //                var fujian = new Domain_MyKaoHeBiaotwo_FuJians();
                //                fujian.bz = ssfujian.bz;
                //                fujian.fileHash = ssfujian.fileHash;
                //                fujian.fileguid = ssfujian.fileguid;
                //                fujian.fjid = ssfujian.fjid;
                //                fujian.fjmc = ssfujian.fjmc;
                //                fujian.path = ssfujian.path;
                //                ss.MyKaoHeBiaotwoFuJianses.Add(fujian);
                //            }
                //            s.MyKaoHeBiaoTwos.Add(ss);
                //        }
                //        model.MyKaoHeBiaoOnes.Add(s);
                //    }

                //}
                Domain_MyKaoHeBiao model = new Domain_MyKaoHeBiao();
                return JsonConvert.SerializeObject(model);
            }
        }

        public void SavePingShengJieGuo(string str)
        {
            List<Domain_MyKaoHeBiaotwo> twos = new List<Domain_MyKaoHeBiaotwo>();
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                Domain_MyKaoHeBiao model = JsonConvert.DeserializeObject<Domain_MyKaoHeBiao>(str);
                foreach (var s in model.MyKaoHeBiaoOnes)
                {
                    twos.AddRange(s.MyKaoHeBiaoTwos);
                }

                var data = db.MyKaoHeBiaoContext.SingleOrDefault(s => s.mkhbid == model.mkhbid);
                foreach (var s in data.MyKaoHeBiaoOnes)
                {
                    foreach (var p in s.MyKaoHeBiaoTwos)
                    {
                        var two = twos.SingleOrDefault(t => t.twoid == p.twoid);
                        if (two != null && two.MyKaoHeBiaoTwoPingFens != null)
                        {
                            var pf = two.MyKaoHeBiaoTwoPingFens.FirstOrDefault();
                            if (p.MyKaoHeBiaoTwoPingFens == null || p.MyKaoHeBiaoTwoPingFens.Count == 0)
                            {
                                p.MyKaoHeBiaoTwoPingFens.Add(new MyKaoHeBiao_twoPingFen() { cjsj = DateTime.Now, fz = pf.fz, pfusername = pf.pfusername, MyKaoHeBiaoTwo = p });
                            }
                            else
                            {
                                if (p.MyKaoHeBiaoTwoPingFens.Any(t => pf != null && t.pfusername != pf.pfusername))
                                {
                                    p.MyKaoHeBiaoTwoPingFens.Add(new MyKaoHeBiao_twoPingFen() { cjsj = DateTime.Now, fz = pf.fz, pfusername = pf.pfusername, MyKaoHeBiaoTwo = p });
                                }
                            }

                        }
                    }
                }

                db.SaveChanges();
            }
        }

        public void CreateFuJiaXiang(string fjxmc, decimal fz)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                FuJiaXiang data = new FuJiaXiang();
                data.fjxfz = fz;
                data.fjxmc = fjxmc;
                db.FuJiaXiangContext.Add(data);
                db.SaveChanges();
            }
        }

        public void UpdateFuJiaXiang(int fjxid, string fjxmc, decimal fz)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                FuJiaXiang data = db.FuJiaXiangContext.SingleOrDefault(s => s.fjxid == fjxid);
                data.fjxfz = fz;
                data.fjxmc = fjxmc;
                db.SaveChanges();
            }
        }

        public void DelFuJiaXiang(int fjxid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                FuJiaXiang data = db.FuJiaXiangContext.SingleOrDefault(s => s.fjxid == fjxid);
                db.Entry(data).State = EntityState.Deleted;
                db.SaveChanges();
            }
        }

        public string GetFuJiaXiangs()
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var datas = db.FuJiaXiangContext;
                List<Domain_FuJiaXiang> lst = new List<Domain_FuJiaXiang>();
                foreach (var item in datas)
                {
                    lst.Add(new Domain_FuJiaXiang() { fjxid = item.fjxid, fjxmc = item.fjxmc, fjxfz = item.fjxfz });
                }
                return JsonConvert.SerializeObject(lst);
            }
        }

        public string GetFuJiaXiang(int fjxid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                FuJiaXiang data = db.FuJiaXiangContext.SingleOrDefault(s => s.fjxid == fjxid);
                Domain_FuJiaXiang model = new Domain_FuJiaXiang();
                model.fjxmc = data.fjxmc;
                model.fjxfz = data.fjxfz;
                model.fjxid = data.fjxid;
                return JsonConvert.SerializeObject(model);
            }
        }

        public void MyKaoHeBiaoAddFuJiaXiang(string username, int mkhbid, int fjxid, decimal fz)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var fjx = db.FuJiaXiangContext.SingleOrDefault(s => s.fjxid == fjxid);
                var data = db.MyKaoHeBiaoContext.SingleOrDefault(s => s.mkhbid == mkhbid);
                if (data.MyKaoHeBiaoFujiaxiangs == null)
                {
                    data.MyKaoHeBiaoFujiaxiangs = new List<MyKaoHeBiao_fujiaxiang>();
                }
                MyKaoHeBiao_fujiaxiang myKaoHeBiaoFujiaxiang = new MyKaoHeBiao_fujiaxiang();
                myKaoHeBiaoFujiaxiang.MyKaoHeBiao = data;
                myKaoHeBiaoFujiaxiang.cjrusername = username;
                myKaoHeBiaoFujiaxiang.xmfz = fz;
                myKaoHeBiaoFujiaxiang.xmid = fjxid;
                myKaoHeBiaoFujiaxiang.xmmc = fjx.fjxmc;
                data.MyKaoHeBiaoFujiaxiangs.Add(myKaoHeBiaoFujiaxiang);
                db.SaveChanges();
            }
        }

        public string GetMyKaoHeBiaoFuJiaXiangs(int mkhbid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.MyKaoHeBiaoContext.SingleOrDefault(s => s.mkhbid == mkhbid);
                List<Domain_MyKaoHeBiaoFuJiaBiao> lst = new List<Domain_MyKaoHeBiaoFuJiaBiao>();
                foreach (var item in data.MyKaoHeBiaoFujiaxiangs)
                {
                    Domain_MyKaoHeBiaoFuJiaBiao model = new Domain_MyKaoHeBiaoFuJiaBiao();
                    model.fjxid = item.fjxid;
                    model.cjrusername = item.cjrusername;
                    model.xmfz = item.xmfz;
                    model.xmid = item.xmid;
                    model.xmmc = item.xmmc;
                    lst.Add(model);
                }
                return JsonConvert.SerializeObject(lst);
            }
        }

        public void MyKaoHeBiaoDelFuJiaXiang(int fjxxmid, int mkhbid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.MyKaoHeBiaoContext.SingleOrDefault(s => s.mkhbid == mkhbid);
                var mykaohefjx = data.MyKaoHeBiaoFujiaxiangs.SingleOrDefault(s => s.fjxid == fjxxmid);
                db.Entry(mykaohefjx).State = EntityState.Deleted;
                db.SaveChanges();
            }
        }

        public string GetKaoHeHuiZongMoBan(int mbid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.GongZuoJiHuaContext.SingleOrDefault(s => s.mbid == mbid);
                if (data != null)
                {
                    Domain_HuiZongMoBan model = new Domain_HuiZongMoBan();
                    foreach (var item in data.GongZuoJiHuaNeiRongs)
                    {
                        model.DomainHuiZongSons.Add(new Domain_HuiZongSon() { sx = item.nrid, gznr = item.nr, nrid = item.ljid });
                    }
                    return JsonConvert.SerializeObject(model);
                }
                else
                {
                    return string.Empty;
                }
            }
        }

        public string GetKaoHeHuiZongDeFen(int mbid, string datetimes)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var zw = db.ZhiWeiContext.ToList();
                DateTime dateTime = DateTime.Parse(datetimes);
                var data = db.MyKaoHeBiaoContext.Where(s => s.mbid == mbid && s.sssj.Year == dateTime.Year && s.sssj.Month == dateTime.Month);
                List<Domain_HuiZongDeFen> lst = new List<Domain_HuiZongDeFen>();
                foreach (var item in data)
                {
                    Domain_HuiZongDeFen model = new Domain_HuiZongDeFen();
                    var userinfo = db.JiaoZhiGongInfoContext.SingleOrDefault(s => s.Users.username == item.username);
                    if (userinfo != null)
                    {
                        model.mkhbid = item.mkhbid;
                        model.xingming = userinfo.XingMing;
                        model.keshi = userinfo.keshi.ksmc;
                        model.zhiwei = userinfo.kszw.zhiwu.zwmc;
                        model.zhanghao = userinfo.Users.username;
                        var linshigongzuo = item.MyKaoHeBiaoOnes.FirstOrDefault(s => s.mbnr == "临时工作");
                        var linshilist = new List<string>();
                        if (linshigongzuo != null && linshigongzuo.MyKaoHeBiaoTwos != null)
                        {
                            var i = 1;
                            foreach (var item2 in linshigongzuo.MyKaoHeBiaoTwos)
                            {

                                linshilist.Add("<p>" + i + "." + item2.mbnr + "</p>");
                                i++;
                            }

                            model.linshigongzuo = string.Join("", linshilist.ToArray());
                        }

                        model.Domain_DeFens = new List<Domain_DeFen>();
                        foreach (var item2 in item.MyKaoHeBiaoFujiaxiangs)
                        {
                            model.fujiaxiang += item2.xmfz;
                        }
                        foreach (var item2 in item.MyKaoHeBiaoOnes)
                        {
                            Domain_DeFen model2 = new Domain_DeFen();
                            if (item2.MyKaoHeBiaoTwos != null && item2.MyKaoHeBiaoTwos.Count > 0)
                            {
                                foreach (var item3 in item2.MyKaoHeBiaoTwos)
                                {
                                    decimal fz = item3.MyKaoHeBiaoTwoPingFens.Sum(s => s.fz);
                                    int c = item3.MyKaoHeBiaoTwoPingFens.Count;
                                    if (c == 0)
                                    {
                                        model2.defen = model2.defen + 0;
                                    }
                                    else
                                    {
                                        decimal f = fz / (decimal)c;
                                        decimal d = decimal.Round(f, 2);
                                        model2.defen = model2.defen + d;
                                    }
                                }
                            }
                            else
                            {
                                model2.defen = item2.fz;
                            }
                            model2.gznr = item2.mbnr;
                            model2.nrid = item2.mbnrid;
                            model2.sx = item.mkhbid;
                            model.Domain_DeFens.Add(model2);
                        }
                        lst.Add(model);
                    }
                }

                return JsonConvert.SerializeObject(lst);
            }
        }

        public void DelPingFen(int mkhbid, string username)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.MyKaoHeBiaoContext.SingleOrDefault(s => s.mkhbid == mkhbid);
                foreach (var item in data.MyKaoHeBiaoOnes)
                {
                    foreach (var item2 in item.MyKaoHeBiaoTwos)
                    {
                        if (item2.MyKaoHeBiaoTwoPingFens != null)
                        {
                            foreach (var item3 in item2.MyKaoHeBiaoTwoPingFens.Where(s => s.pfusername == username).ToArray())
                            {
                                db.Entry(item3).State = EntityState.Deleted;
                            }
                        }
                    }
                }
                db.SaveChanges();
            }
        }

        public string GetPingFenRenYuanList(int mkhbid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.MyKaoHeBiaoContext.SingleOrDefault(s => s.mkhbid == mkhbid);
                List<Domain_MyKaoHeBiaoPingFen> lst = new List<Domain_MyKaoHeBiaoPingFen>();
                foreach (var item in data.MyKaoHeBiaoOnes)
                {
                    foreach (var item2 in item.MyKaoHeBiaoTwos)
                    {
                        foreach (var item3 in item2.MyKaoHeBiaoTwoPingFens)
                        {
                            if (lst.All(s => s.pfusername != item3.pfusername))
                            {
                                lst.Add(new Domain_MyKaoHeBiaoPingFen() { fz = item3.fz, XingMing = "", pfusername = item3.pfusername, cjsj = item3.cjsj, pfid = item3.pfid });
                            }
                        }
                    }
                }

                foreach (var item in lst)
                {
                    item.XingMing = db.JiaoZhiGongInfoContext.SingleOrDefault(s => s.Users.username == item.pfusername)
                        .XingMing;
                }
                return JsonConvert.SerializeObject(lst);
            }
        }

        public string GetBieKaoHeRenYuans(string username)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                //TODO 过滤考核过的人员
                var 被考核人 = db.KaoHeFanWeiRenYuansContext.Single(s => s.username == username);
                Guid pjrid = db.JiaoZhiGongInfoContext.Single(s => s.Users.username == username).Users.userinfoId;
                var new被考核人 = new List<BeiKaoHeJiaoZhiGong>();
                if (被考核人.JiaoZhiGongInfos != null)
                {
                    foreach (var renyuan in 被考核人.JiaoZhiGongInfos)
                    {
                        Guid userinfoId = Guid.Parse(renyuan.userinfoId);
                        var 考核表 = db.PersonalMainContext.Where(s => s.ssrid == userinfoId && s.state == 考核表状态.通过审核).ToList();
                        bool flag = true;
                        foreach (var item in 考核表)
                        {
                            foreach (var item2 in item.PersonalPath)
                            {
                                foreach (var item3 in item2.PersonalContents)
                                {
                                    if (item3.PersonalEvaluates.Any(s => s.pjrid == pjrid))
                                    {
                                        flag = false;
                                        break;
                                    }
                                }
                                if (!flag)
                                {
                                    break;
                                }
                            }
                            if (flag)
                            {
                                if (new被考核人.Any(s => s.userinfoId == renyuan.userinfoId))
                                {

                                }
                                else
                                {
                                    new被考核人.Add(renyuan);
                                }

                            }
                        }
                    }
                    return JsonConvert.SerializeObject(new被考核人);
                }

                return string.Empty;
            }
        }

        public string GetKaoHeBiaos(string username, string khusername)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                //TODO 过滤考核过的考核表
                var userinfo = db.JiaoZhiGongInfoContext.Single(s => s.Users.username == username);
                Guid pjrid = db.JiaoZhiGongInfoContext.Single(s => s.Users.username == khusername).Users.userinfoId;
                var 考核表 = db.PersonalMainContext.Where(s => s.ssrid == userinfo.Users.userinfoId && s.state == 考核表状态.通过审核).ToList();
                var 考核范围 = db.KaoHeFanWeiRenYuansContext.Single(s => s.username == khusername).NeiRongs
                    .Select(s => s.ljid).ToList();
                if (考核表.Count == 0)
                {
                    return string.Empty;
                }
                var new考核表 = new List<PersonalMain>();
                bool flag = true;
                foreach (var item in 考核表)
                {
                    PersonalMain model = new PersonalMain();
                    model.state = item.state;
                    model.PersonalPath = new List<PersonalPath>();
                    model.mobanid = item.mobanid;
                    model.pmid = item.pmid;
                    model.ssrid = item.ssrid;
                    model.AdditionalItems = new List<AdditionalItems>();
                    model.PersonalWorkSummary = new PersonalWorkSummary();
                    model.StateLog = new List<PersonalMainStateLog>();
                    model.cjsj = item.cjsj;
                    model.sssj = item.sssj;
                    model.tableName = item.tableName;

                    foreach (var item2 in item.PersonalPath)
                    {
                        if (考核范围.Any(s => s == item2.mpid))
                        {

                            foreach (var item3 in item2.PersonalContents)
                            {
                                if (item3.PersonalEvaluates.Any(s => s.pjrid == pjrid))
                                {
                                    flag = false;//false 评价过
                                    break;
                                }
                            }
                            if (!flag)
                            {
                                break;
                            }
                            else
                            {
                                model.PersonalPath.Add(item2);
                            }
                        }


                    }
                    if (flag)
                    {
                        new考核表.Add(model);
                    }
                    flag = true;
                }
                List<PersonalMain> lst = new List<PersonalMain>();
                foreach (var item in new考核表)
                {
                    var model = Mapping.ToPersonalMain(item);
                    lst.Add(model);
                }

                return JsonConvert.SerializeObject(lst);
            }
        }

        public string GetKaoHeBiaoInfo(int khbid, string username)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var userinfo = db.JiaoZhiGongInfoContext.Single(s => s.Users.username == username);
                var 考核人 = db.KaoHeFanWeiRenYuansContext.Single(s => s.username == username);
                var mpids = 考核人.NeiRongs.Select(s => s.nrid).ToArray();
                var 考核表 = db.PersonalMainContext.Single(s => s.pmid == khbid);
                var t1 = 考核表.PersonalPath.Where(s => mpids.Contains(s.mpid));
                var t2 = new List<PersonalPath>();
                foreach (var item in t1.ToList())
                {
                    PersonalPath pp = new PersonalPath();
                    pp = item;
                    pp.PersonalContents = new List<PersonalContent>();
                    foreach (var item2 in item.PersonalContents.ToList())
                    {
                        if (item2.PersonalEvaluates.Any(s => s.pjrid == userinfo.Users.userinfoId))
                        {

                        }
                        else
                        {
                            pp.PersonalContents.Add(item2);
                        }
                    }
                    t2.Add(pp);
                }
                return JsonConvert.SerializeObject(t2);
            }
        }

        public int UpdateDianPing(int khbid, int pcid, decimal fz, string username)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var 考核人 = db.JiaoZhiGongInfoContext.Single(s => s.Users.username == username);
                var 考核表 = db.PersonalMainContext.Single(s => s.pmid == khbid);
                var 考核内容 = 考核表.PersonalPath.SelectMany(s => s.PersonalContents).Single(p => p.pcid == pcid);
                //TODO 加入检测分值是否合理
                PersonalEvaluate pe = new PersonalEvaluate()
                {
                    cjsj = DateTime.Now,
                    fz = fz,
                    iseffective = true,
                    pjrid = 考核人.Users.userinfoId
                };
                if (考核内容.PersonalEvaluates.Any(s => s.pjrid == 考核人.Users.userinfoId))
                {

                }
                else
                {
                    考核内容.PersonalEvaluates.Add(pe);
                }

                db.SaveChanges();
                return pe.pjid;
            }
        }

        public string GetBeiKaoHeBiaos(string username)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var 被考核人 = db.KaoHeFanWeiRenYuansContext.Single(s => s.username == username);
                if (被考核人.JiaoZhiGongInfos != null)
                {
                    List<Guid> beikaoheren = new List<Guid>();
                    foreach (var item in 被考核人.JiaoZhiGongInfos)
                    {
                        beikaoheren.Add(Guid.Parse(item.userinfoId));
                    }
                    var 考核表 = db.PersonalMainContext.Where(s => beikaoheren.Contains(s.ssrid) && s.state == 考核表状态.提交审核).ToList();
                    List<dynamic> models = new List<dynamic>();
                    foreach (var item in 考核表)
                    {
                        dynamic d = new
                        {
                            ssrid = item.ssrid,
                            cjsj = item.cjsj,
                            pmid = item.pmid,
                            tableName = item.tableName,
                            XingMing = db.JiaoZhiGongInfoContext.Single(s => s.Users.userinfoId == item.ssrid).XingMing
                        };
                        models.Add(d);
                    }
                    if (考核表.Count == 0)
                    {
                        return string.Empty;
                    }
                    return JsonConvert.SerializeObject(models);
                }
                return string.Empty;
            }
        }
    }
}
